<template>
  <div class="app-container">

    <el-table
      :data="rootRegion"
      row-key="bh"
      lazy
      :load="loadRegionTree"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="名称" align="left" prop="name"/>
      <el-table-column label="区划代码" align="center" prop="bh"/>
      <el-table-column label="父区划代码" prop="pbh"/>
      <el-table-column label="全称" align="center" prop="fullname"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
            v-hasPermi="['biz:region:query']"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查看区划管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="区划代码" prop="bh">
          <el-input v-model="form.bh" :disabled="true"/>
        </el-form-item>
        <el-form-item label="父区划代码" prop="pbh">
          <el-input v-model="form.pbh" :disabled="true"/>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" :disabled="true"/>
        </el-form-item>
        <el-form-item label="全称" prop="fullname">
          <el-input v-model="form.fullname" :disabled="true"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listRegion, getRegion} from "@/api/system/region";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Region",
  components: {
  },
  data() {
    return {
      rootRegion: [{
        bh: this.$store.getters.regionBh,
        pbh: null,
        name: this.$store.getters.regionName,
        fullname: this.$store.getters.regionFullname,
        hasChildren: true
      }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  mounted() {
    this.expanded();
  },
  methods: {
    /** 展开表格树结构第一层 */
    expanded(){
      const els = document.getElementsByClassName('el-table__expand-icon');
      this.$nextTick(()=>{
        els[0].click()
      })
    },
    /** 懒加载区划树 */
    loadRegionTree(tree, treeNode, resolve) {
      listRegion(tree.bh).then(res => {
        if (res.code === 200) {  //后台返回是用code表示返回状态代码，与后台匹配
          let data = res.data
          data.forEach(element => {
            element['qh'] = element.bh
            element['pbh'] = element.pbh
            element['name'] = element.name
            element['fullname'] = element.fullname
            if (element.bh.length < 6) {
              element['hasChildren'] = true
            } else {
              element['hasChildren'] = false
            }
          })
          resolve(data);
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bh: null,
        pbh: null,
        name: null,
        fullname: null
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleDetail(row) {
      this.reset();
      if (row != null) {
        this.form.pbh = row.bh;
      }
      getRegion(row.bh).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看详情";
      });
    }
  }
};
</script>
